// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
const db = uniCloud.database();
const dbCmd = db.command
module.exports = {
	_before: function() { // 通用预处理器

	},
	async like_article(user_id, article_id) {
		const userinfo =await db.collection('uni-id-users').where({
			_id: user_id
		}).get()
		const article_id_ids = userinfo.data[0].article_likes_ids
		let dbCmdFuns = null
		if (article_id_ids.includes(article_id)) {
			dbCmdFuns = dbCmd.pull(article_id)
		} else {
			dbCmdFuns = dbCmd.addToSet(article_id)
		}

		await db.collection('uni-id-users').doc(user_id).update({
			article_likes_ids: dbCmdFuns
		})
		//event为客户端上传的参数
		//返回数据给客户端
		return {
			code: 200,
			msg: '数据请求成功',
			data: userinfo.data[0]
		}
	},
	async getmyCollectArticle(article_like_ids){
		const articles =await db.collection('opendb-news-articles').where({
			_id: dbCmd.in(article_like_ids)
		}).get()
		
		return {
			code: 200,
			msg: '数据请求成功',
			data: articles.data[0]
		}
	}
	
	
}